package com.baihan.angus.app.game.basic.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import android.util.Log;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.configs.ConfigConstants;

import java.util.Locale;

/**
 * 补充说明：app语言种类切换工具
 */
public class AppLanguageUtil {

    private static final String TAG = "LanguageUtil";
    public static final String LOCAL_ENG = "en-us";
    public static final String LOCAL_CHN = "zh-cn";

    /**
     * @param context
     * @param languageCode 想要切换的语言类型 比如 "en" ,"zh"
     */
    @SuppressWarnings("deprecation")
    public static void changeAppLanguage(Context context, int languageCode) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        //获取想要切换的语言类型
        Locale locale = getLocaleByLanguage(languageCode);
        configuration.setLocale(locale);
        // updateConfiguration
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
    }

    public static Locale getLocaleByLanguage(int language) {
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        if (language == ConfigConstants.LANGUAGE_SIMPLE_CHN) {
            locale = Locale.SIMPLIFIED_CHINESE;
        } else if (language == ConfigConstants.LANGUAGE_SIMPLE_ENG) {
            locale = Locale.ENGLISH;
        }
        Log.d(TAG, "getLocaleByLanguage: " + locale.getDisplayName());
        return locale;
    }

    public static Context attachBaseContext(Context context, int languageCode) {
        Log.d(TAG, "attachBaseContext: " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, languageCode);
        } else {
            return context;
        }
    }

    public static void updateApplicationConfiguration(int languageCode) {
        if (BaseApplication.getApplication() != null) {
            Configuration configuration = getNewConfiguration(BaseApplication.getApplication(), languageCode);
            BaseApplication.getApplication().getApplicationContext().getResources().updateConfiguration(configuration, BaseApplication.getApplication().getApplicationContext().getResources().getDisplayMetrics());
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Configuration getNewConfiguration(Context context, int languageCode) {
        Resources resources = context.getResources();
        Locale locale = getLocaleByLanguage(languageCode);
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return configuration;
    }

    private static Context updateResources(Context context, int languageCode) {
        Configuration configuration = getNewConfiguration(context, languageCode);
        return context.createConfigurationContext(configuration);
    }

    public static String getLocalStr(int code) {
        switch (code) {
            case ConfigConstants.LANGUAGE_SIMPLE_ENG:
                return LOCAL_ENG;
            case ConfigConstants.LANGUAGE_SIMPLE_CHN:
            default:
                return LOCAL_CHN;
        }
    }
}
